{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Discord, Slack, and Telegram messaging tools\n",
    "\n",
    "In this notebook we introduce send and retrieve message tools for 3 messaging platforms: Discord, Slack, and Telegram.\n",
    "\n",
    "These tools can be added to your AG2 agents, enabling them to send messages during a workflow and/or retrieve messages for use by the workflow.\n",
    "\n",
    "There are two types of tools, with one for each platform:\n",
    "\n",
    "- Sending a message (`DiscordSendTool`, `SlackSendTool`, `TelegramSendTool`)\n",
    "- Retrieving messages (`DiscordRetrieveTool`, `SlackRetrieveTool`, `TelegramRetrieveTool`)\n",
    "\n",
    "You can attach one or more of them to an agent, though it may be most typical that you create an agent for each platform and attach their respective send and/or retrieve tools. This would allow you to tailor your system message for your agent to the messaging style used for the platform."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "````mdx-code-block\n",
    ":::note\n",
    "These tools are currently in our `experimental` namespace, indicating that we have tested the functionality but they have not been confirmed for use in production. Please use them with that in mind and we appreciate any feedback on them.\n",
    "\n",
    "If you do find any bugs please [log an issue](https://github.com/ag2ai/ag2/issues) in the AG2 repository. If you would like to add more tools or functionality, we would love your [contribution](https://docs.ag2.ai/latest/docs/contributor-guide/contributing).\n",
    ":::\n",
    "````"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation\n",
    "\n",
    "Each messaging platform requires installation of a package specifically for that platform.\n",
    "\n",
    "To use the Discord tools install AG2 with the `commsagent-discord` extra:\n",
    "```bash\n",
    "pip install -U ag2[openai,commsagent-discord]\n",
    "```\n",
    "\n",
    "Similarly, for slack and telegram:\n",
    "```bash\n",
    "pip install -U ag2[openai,commsagent-slack]\n",
    "```\n",
    "and\n",
    "```bash\n",
    "pip install -U ag2[openai,commsagent-telegram]\n",
    "```\n",
    "\n",
    "You're all set! Now you can start sending to and retrieving messages from these platforms."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### LLM Configuration\n",
    "\n",
    "The examples in this notebook will use OpenAI's `GPT-4o mini` model and the `OPENAI_API_TOKEN` will be retrieved from environment variables, we can use an LLM configuration as follows:\n",
    "\n",
    "```python\n",
    "llm_config = {\"model\": \"gpt-5-nano\", \"api_type\": \"openai\"}\n",
    "```\n",
    "\n",
    "For further information on LLM configurations and how to set your environment variables up, check out our [Basic Concepts guide](https://docs.ag2.ai/latest/docs/user-guide/basic-concepts/llm-configuration)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Protecting details from the LLM\n",
    "\n",
    "These tools utilise dependency injection to protect your authentication and channel/group details from being sent to the LLM.\n",
    "\n",
    "For more about AG2's dependency injection, see [this notebook](https://docs.ag2.ai/latest/docs/use-cases/notebooks/notebooks/tools_dependency_injection)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Discord Send/Retrieve Messages\n",
    "\n",
    "### Tokens, Guilds, and Channels\n",
    "The Discord tools, `DiscordSendTool` and `DiscordRetrieveTool`, require authentication (bot token), server (guild name), and channel details in order to send/retrieve messages.\n",
    "\n",
    "Here are some references to help you establish those details:\n",
    "\n",
    "- [Creating a Bot Account](https://discordpy.readthedocs.io/en/latest/discord.html), and [another reference](https://gist.github.com/SinisterRectus/2d84025f51bc0be37d2f0d0c559c2e35#how-do-i-make-a-discord-bot)\n",
    "- [Adding a Bot to a server](https://gist.github.com/SinisterRectus/2d84025f51bc0be37d2f0d0c559c2e35#how-do-i-add-a-discord-bot-to-my-server)\n",
    "- Bot Permissions you will need:\n",
    "  - General Permissions: View Channels\n",
    "  - Text Permissions: Send Messages, Read Message History, Use Slash Commands\n",
    "- Server/Guild Name - Right-click the Server > Server Settings > Overview, see the Server Name at the top right (e.g. \"My Test Server\")\n",
    "- Channel name - The text after the hash (e.g. \"general\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sending and retrieving messages with Discord\n",
    "\n",
    "Let's add the Discord Send and Retrieve tools to an agent and have them executed by another agent in a simple 2-agent chat."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tools are available in the autogen.tools namespace\n",
    "from autogen import ConversableAgent\n",
    "from autogen.tools.experimental import DiscordRetrieveTool, DiscordSendTool\n",
    "\n",
    "llm_config = {\"model\": \"gpt-5-nano\", \"api_type\": \"openai\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create our two agents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Our tool executor agent, which will run the tools once recommended by the discord_agent, no LLM required\n",
    "executor_agent = ConversableAgent(\n",
    "    name=\"executor_agent\",\n",
    "    human_input_mode=\"NEVER\",\n",
    ")\n",
    "\n",
    "# Our discord agent, who will construct messages and recommend the tool calls\n",
    "discord_agent = ConversableAgent(\n",
    "    name=\"discord_agent\",\n",
    "    llm_config=llm_config,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Prepare our authentication, server/guild, channel details, then create our tools, and finally register them with the agents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_bot_token = \"MTMyOTI...\"  # Discord bot token\n",
    "_guild_name = \"My Test Server\"  # Name of the server\n",
    "_channel_name = \"general\"  # Name of the channel, this is equivalent to \"# general\"\n",
    "\n",
    "# Create our send tool\n",
    "discord_send_tool = DiscordSendTool(bot_token=_bot_token, guild_name=_guild_name, channel_name=_channel_name)\n",
    "\n",
    "# Register it for recommendation by our Discord agent\n",
    "discord_send_tool.register_for_llm(discord_agent)\n",
    "\n",
    "# Register it for execution by our executor agent\n",
    "discord_send_tool.register_for_execution(executor_agent)\n",
    "\n",
    "# And the same for our our retrieve tool\n",
    "discord_retrieve_tool = DiscordRetrieveTool(bot_token=_bot_token, guild_name=_guild_name, channel_name=_channel_name)\n",
    "discord_retrieve_tool.register_for_llm(discord_agent)\n",
    "discord_retrieve_tool.register_for_execution(executor_agent)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's send a message to Discord, all about the wonders of Australia.\n",
    "\n",
    "We'll limit it to 2 turns, allowing the Discord agent to receive the request, construct and recommend the send tool, and then the executor agent to execute the tool, sending the message to Discord."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "executor_agent.initiate_chat(\n",
    "    recipient=discord_agent,\n",
    "    message=\"Let's send a message to Discord giving them a paragraph on the highlights of Australia.\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And here's the message it sent:\n",
    "\n",
    "![](https://media.githubusercontent.com/media/ag2ai/ag2/refs/heads/main/notebook/commsplatforms_discord_sentmsg.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the output:\n",
    "\n",
    "```console\n",
    "executor_agent (to discord_agent):\n",
    "\n",
    "Let's send a message to Discord giving them a paragraph on the highlights of Australia.\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "discord_agent (to executor_agent):\n",
    "\n",
    "***** Suggested tool call (call_iLjFWjeDcUt3jEC7JIeU79Kq): discord_send *****\n",
    "Arguments: \n",
    "{\"message\":\"Australia is renowned for its stunning landscapes, diverse wildlife, and vibrant cities. The Great Barrier Reef, the world's largest coral reef system, offers breathtaking underwater vistas and a rich variety of marine life. In addition to its natural wonders, Australia boasts unique landmarks like the Sydney Opera House and Uluru, a sacred site for Indigenous Australians. The country is also home to a rich cultural scene, with various festivals and events celebrating its heritage. Outdoor enthusiasts can enjoy activities ranging from surfing on world-famous beaches to hiking through expansive national parks. Whether you're exploring the bustling streets of Melbourne or relaxing in the serene outback, Australia promises an unforgettable adventure.\"}\n",
    "*****************************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> EXECUTING FUNCTION discord_send...\n",
    "Call ID: call_iLjFWjeDcUt3jEC7JIeU79Kq\n",
    "Input arguments: {'message': \"Australia is renowned for its stunning landscapes, diverse wildlife, and vibrant cities. The Great Barrier Reef, the world's largest coral reef system, offers breathtaking underwater vistas and a rich variety of marine life. In addition to its natural wonders, Australia boasts unique landmarks like the Sydney Opera House and Uluru, a sacred site for Indigenous Australians. The country is also home to a rich cultural scene, with various festivals and events celebrating its heritage. Outdoor enthusiasts can enjoy activities ranging from surfing on world-famous beaches to hiking through expansive national parks. Whether you're exploring the bustling streets of Melbourne or relaxing in the serene outback, Australia promises an unforgettable adventure.\"}\n",
    "executor_agent (to discord_agent):\n",
    "\n",
    "***** Response from calling tool (call_iLjFWjeDcUt3jEC7JIeU79Kq) *****\n",
    "Message sent successfully (ID: 1336541434672844870):\n",
    "Australia is renowned for its stunning landscapes, diverse wildlife, and vibrant cities. The Great Barrier Reef, the world's largest coral reef system, offers breathtaking underwater vistas and a rich variety of marine life. In addition to its natural wonders, Australia boasts unique landmarks like the Sydney Opera House and Uluru, a sacred site for Indigenous Australians. The country is also home to a rich cultural scene, with various festivals and events celebrating its heritage. Outdoor enthusiasts can enjoy activities ranging from surfing on world-famous beaches to hiking through expansive national parks. Whether you're exploring the bustling streets of Melbourne or relaxing in the serene outback, Australia promises an unforgettable adventure.\n",
    "**********************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "discord_agent (to executor_agent):\n",
    "\n",
    "The message about the highlights of Australia has been successfully sent to Discord. If you need anything else, feel free to ask!\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's now retrieve the last two messages from the channel, this will include the message we sent as well as a previous one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "executor_agent.initiate_chat(\n",
    "    recipient=discord_agent,\n",
    "    message=\"Tell me which countries the last two messages on my Discord channel are about and the main differences.\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Output:\n",
    "\n",
    "```console\n",
    "executor_agent (to discord_agent):\n",
    "\n",
    "Tell me which countries the last two messages on my Discord channel are about and the main differences.\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "discord_agent (to executor_agent):\n",
    "\n",
    "***** Suggested tool call (call_slGKL1HRJfcCb3kNsmd0JcC0): discord_retrieve *****\n",
    "Arguments: \n",
    "{\"maximum_messages\":2}\n",
    "*********************************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> EXECUTING FUNCTION discord_retrieve...\n",
    "Call ID: call_slGKL1HRJfcCb3kNsmd0JcC0\n",
    "Input arguments: {'maximum_messages': 2}\n",
    "executor_agent (to discord_agent):\n",
    "\n",
    "***** Response from calling tool (call_slGKL1HRJfcCb3kNsmd0JcC0) *****\n",
    "[{'id': '1336541434672844870', 'content': \"Australia is renowned for its stunning landscapes, diverse wildlife, and vibrant cities. The Great Barrier Reef, the world's largest coral reef system, offers breathtaking underwater vistas and a rich variety of marine life. In addition to its natural wonders, Australia boasts unique landmarks like the Sydney Opera House and Uluru, a sacred site for Indigenous Australians. The country is also home to a rich cultural scene, with various festivals and events celebrating its heritage. Outdoor enthusiasts can enjoy activities ranging from surfing on world-famous beaches to hiking through expansive national parks. Whether you're exploring the bustling streets of Melbourne or relaxing in the serene outback, Australia promises an unforgettable adventure.\", 'author': 'AG2CommsAgentWorkflow#8931', 'timestamp': '2025-02-05T03:38:29.765000+00:00'}, {'id': '1336186020642951312', 'content': \"🇯🇵 **Discover Japan: A Blend of Tradition and Modernity!** 🌸  \\n\\nJapan is a fascinating country where ancient traditions coexist with cutting-edge technology. From bustling cities like Tokyo to serene temples in Kyoto, there's something for everyone! 🏯🚄  \\n\\n**Did you know?**  \\n- Japan has the world's oldest continuously operating company, established in 578 AD! 🏢  \\n- It's home to the famous cherry blossoms, which attract millions each spring! 🌸  \\n- Japanese cuisine, including sushi and ramen, is loved worldwide! 🍣🍜  \\n\\nWhat’s your favorite thing about Japan? Share your thoughts below! 👇\", 'author': 'AG2CommsAgentWorkflow#8931', 'timestamp': '2025-02-04T04:06:12.457000+00:00'}]\n",
    "**********************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "discord_agent (to executor_agent):\n",
    "\n",
    "The last two messages in your Discord channel are about Australia and Japan. Here are the main differences between the two countries based on the content of the messages:\n",
    "\n",
    "### Australia\n",
    "- Noted for its **stunning landscapes** and **diverse wildlife**.\n",
    "- Highlights the **Great Barrier Reef**, a natural wonder and the world's largest coral reef system.\n",
    "- Features unique landmarks like the **Sydney Opera House** and **Uluru**, which is significant for Indigenous Australians.\n",
    "- Celebrates a **rich cultural scene** with various festivals and events.\n",
    "- Provides outdoor activities such as **surfing** and **hiking** through national parks.\n",
    "- Offers a mix of urban life in cities like **Melbourne** and adventures in the **outback**.\n",
    "\n",
    "### Japan\n",
    "- Known for its interesting blend of **ancient traditions** and **modern technology**.\n",
    "- Features bustling cities like **Tokyo** alongside serene locations like **Kyoto** with its temples.\n",
    "- Notable facts include the world's oldest continuously operating company and its famous **cherry blossoms**.\n",
    "- Japanese cuisine, especially **sushi** and **ramen**, is widely cherished around the world.\n",
    "- Engages visitors with a call to share personal favorites about Japan, emphasizing community interaction.\n",
    "\n",
    "In summary, while Australia emphasizes natural beauty and outdoor adventures, Japan focuses on the coexistence of tradition and modernity, along with cultural and culinary significance.\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Slack Send/Retrieve Messages\n",
    "\n",
    "### Tokens and Channels\n",
    "The Slack tools, `SlackSendTool` and `SlackRetrieveTool`, require authentication (OAuth token) and channel details in order to send/retrieve messages.\n",
    "\n",
    "Here are the steps to get a token:\n",
    "\n",
    "- Create an app, from scratch, at https://api.slack.com/apps\n",
    "- In your app settings, go to Features > OAuth & Permissions:\n",
    "  - Under \"Bot Token Scopes\", add these permissions:  \n",
    "    - chat:write (to send messages) \n",
    "    - channels:history \n",
    "    - channels:read (to access channel info) \n",
    "    - groups:read (for private channels)\n",
    "    - im:read (for direct messages) \n",
    "    - users:read (to get user info) \n",
    "    - files:read (to access file attachments) \n",
    "    - groups:history \n",
    "    - im:history \n",
    "    - mpim:history\n",
    "- With your app setup, now install it in your workspace, using the \"Install App\" menu, to create an OAuth Token.\n",
    "\n",
    "To get the ID for your channel:\n",
    "\n",
    "- Open Slack in a browser\n",
    "- Navigate to your channel\n",
    "- Get the channel ID from the URL (e.g., .../C12345678)\n",
    "\n",
    "Finally you need to add the bot to your channel:\n",
    "\n",
    "- In Slack, go to your channel\n",
    "- Type `/invite @YourBotName`, e.g. `/invite @ag2commsagent`\n",
    "\n",
    "Now you should be good to go with your OAuth token, channel ID, and a bot on your channel ready to send and retrieve messages!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sending and retrieving messages with Slack\n",
    "\n",
    "Let's add the Slack Send and Retrieve tools to an agent and have them executed by another agent in as simple 2-agent chat.\n",
    "\n",
    "This time, we'll include a weather tool so that we can send a weather forecast."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tools are available in the autogen.tools namespace\n",
    "from autogen import ConversableAgent, register_function\n",
    "from autogen.tools.experimental import SlackRetrieveTool, SlackSendTool\n",
    "\n",
    "llm_config = {\"model\": \"gpt-5-nano\", \"api_type\": \"openai\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create our two agents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Our tool executor agent, which will run the tools once recommended by the slack_agent, no LLM required\n",
    "executor_agent = ConversableAgent(\n",
    "    name=\"executor_agent\",\n",
    "    human_input_mode=\"NEVER\",\n",
    ")\n",
    "\n",
    "slack_agent = ConversableAgent(\n",
    "    name=\"slack_agent\",\n",
    "    llm_config=llm_config,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Prepare our authentication and channel details, then create our tools, and finally register them with the agents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_bot_token = \"xoxo...\"  # OAuth token\n",
    "_channel_id = \"C1234567\"  # ID of the Slack channel\n",
    "\n",
    "# Create our send tool\n",
    "slack_send_tool = SlackSendTool(bot_token=_bot_token, channel_id=_channel_id)\n",
    "\n",
    "# Register it for recommendation by our Slack agent\n",
    "slack_send_tool.register_for_llm(slack_agent)\n",
    "\n",
    "# Register it for execution by our executor agent\n",
    "slack_send_tool.register_for_execution(executor_agent)\n",
    "\n",
    "# And the same for our our retrieve tool\n",
    "slack_retrieve_tool = SlackRetrieveTool(bot_token=_bot_token, channel_id=_channel_id)\n",
    "slack_retrieve_tool.register_for_llm(slack_agent)\n",
    "slack_retrieve_tool.register_for_execution(executor_agent)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's our weather tool and we'll register it to the two agents, but this time using the `register_function` method to do it in one go."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Weather function\n",
    "def get_weather():\n",
    "    return \"The weather today is 25 degrees Celsius and sunny, with a late storm.\"\n",
    "\n",
    "\n",
    "register_function(\n",
    "    get_weather,\n",
    "    caller=slack_agent,\n",
    "    executor=executor_agent,\n",
    "    description=\"Get the current weather forecast\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's send a message to Slack, giving everyone a weather forecast for today.\n",
    "\n",
    "We'll limit it to 3 turns, allowing the Slack agent to receive the request, recommend the weather tool, and go around the agents and then construct and recommend the send tool. The executor agent will execute each tool request."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "executor_agent.initiate_chat(\n",
    "    recipient=slack_agent,\n",
    "    message=\"Get the latest weather forecast and send it to our Slack channel. Use some emojis to make it fun!\",\n",
    "    max_turns=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And here's the message it sent:\n",
    "\n",
    "![](https://media.githubusercontent.com/media/ag2ai/ag2/refs/heads/main/notebook/commsplatforms_slack_sentmsg.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the output:\n",
    "\n",
    "```console\n",
    "executor_agent (to slack_agent):\n",
    "\n",
    "Get the latest weather forecast and send it to our Slack channel. Use some emojis to make it fun!\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "slack_agent (to executor_agent):\n",
    "\n",
    "***** Suggested tool call (call_PCE5cFUXFJ0jFCRj4Zva0yP8): get_weather *****\n",
    "Arguments: \n",
    "{}\n",
    "****************************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> EXECUTING FUNCTION get_weather...\n",
    "Call ID: call_PCE5cFUXFJ0jFCRj4Zva0yP8\n",
    "Input arguments: {}\n",
    "executor_agent (to slack_agent):\n",
    "\n",
    "***** Response from calling tool (call_PCE5cFUXFJ0jFCRj4Zva0yP8) *****\n",
    "The weather today is 25 degrees Celsius and sunny, with a late storm.\n",
    "**********************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "slack_agent (to executor_agent):\n",
    "\n",
    "***** Suggested tool call (call_IdyhjnQ5YGhxfjbDAyKqy1nb): slack_send *****\n",
    "Arguments: \n",
    "{\"message\":\"☀️🌤️ Good morning! The weather today is looking great! It's a sunny 25°C 🌡️. But don’t forget, there might be a late storm 🌩️ later in the day! Stay safe and enjoy your day! 🌈😊\"}\n",
    "***************************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> EXECUTING FUNCTION slack_send...\n",
    "Call ID: call_IdyhjnQ5YGhxfjbDAyKqy1nb\n",
    "Input arguments: {'message': \"☀️🌤️ Good morning! The weather today is looking great! It's a sunny 25°C 🌡️. But don’t forget, there might be a late storm 🌩️ later in the day! Stay safe and enjoy your day! 🌈😊\"}\n",
    "executor_agent (to slack_agent):\n",
    "\n",
    "***** Response from calling tool (call_IdyhjnQ5YGhxfjbDAyKqy1nb) *****\n",
    "Message sent successfully (ID: 1738728329.265739):\n",
    "☀️🌤️ Good morning! The weather today is looking great! It's a sunny 25°C 🌡️. But don’t forget, there might be a late storm 🌩️ later in the day! Stay safe and enjoy your day! 🌈😊\n",
    "**********************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "slack_agent (to executor_agent):\n",
    "\n",
    "I've sent the latest weather forecast to your Slack channel! Here's the message: \n",
    "\n",
    "☀️🌤️ Good morning! The weather today is looking great! It's a sunny 25°C 🌡️. But don’t forget, there might be a late storm 🌩️ later in the day! Stay safe and enjoy your day! 🌈😊\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's now retrieve the last 5 messages to give a picture of the weather for the last week."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "executor_agent.initiate_chat(\n",
    "    recipient=slack_agent,\n",
    "    message=\"Get the last 5 messages about daily weather from our Slack channel and give me a summary of the week's weather.\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Output:\n",
    "\n",
    "```console\n",
    "executor_agent (to slack_agent):\n",
    "\n",
    "Get the last 5 messages about daily weather from our Slack channel and give me a summary of the week's weather.\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "slack_agent (to executor_agent):\n",
    "\n",
    "***** Suggested tool call (call_NDb8KJXEAdsetPvYvXkM6BKM): slack_retrieve *****\n",
    "Arguments: \n",
    "{\"maximum_messages\":5}\n",
    "*******************************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> EXECUTING FUNCTION slack_retrieve...\n",
    "Call ID: call_NDb8KJXEAdsetPvYvXkM6BKM\n",
    "Input arguments: {'maximum_messages': 5}\n",
    "executor_agent (to slack_agent):\n",
    "\n",
    "***** Response from calling tool (call_NDb8KJXEAdsetPvYvXkM6BKM) *****\n",
    "{'message_count': 5, 'messages': [{'user': 'U088V1NKK3R', 'type': 'message', 'ts': '1738728764.548459', 'bot_id': 'B089C36A1B6', 'app_id': 'A0892UE22GN', 'text': \":sun_with_face: Good morning, everyone! Today's weather is a sizzling 30°C! :sunny::fire: Stay hydrated and take care, especially with the high fire danger. :ocean::no_entry_sign: Let's keep it safe out there!\", 'team': 'T088N5HLRTR', 'bot_profile': {'id': 'B089C36A1B6', 'deleted': False, 'name': 'ag2commsagent', 'updated': 1737225262, 'app_id': 'A0892UE22GN', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'team_id': 'T088N5HLRTR'}, 'blocks': [{'type': 'rich_text', 'block_id': '6gpG', 'elements': [{'type': 'rich_text_section', 'elements': [{'type': 'emoji', 'name': 'sun_with_face', 'unicode': '1f31e'}, {'type': 'text', 'text': \" Good morning, everyone! Today's weather is a sizzling 30°C! \"}, {'type': 'emoji', 'name': 'sunny', 'unicode': '2600-fe0f'}, {'type': 'emoji', 'name': 'fire', 'unicode': '1f525'}, {'type': 'text', 'text': ' Stay hydrated and take care, especially with the high fire danger. '}, {'type': 'emoji', 'name': 'ocean', 'unicode': '1f30a'}, {'type': 'emoji', 'name': 'no_entry_sign', 'unicode': '1f6ab'}, {'type': 'text', 'text': \" Let's keep it safe out there!\"}]}]}]}, {'user': 'U088V1NKK3R', 'type': 'message', 'ts': '1738728751.427209', 'bot_id': 'B089C36A1B6', 'app_id': 'A0892UE22GN', 'text': \":mostly_sunny: Hey team! Today's weather forecast is in: It's a chilly 10°C with some strong cold winds! :cold_face: Don't forget to wrap up warm! :scarf::coffee:\", 'team': 'T088N5HLRTR', 'bot_profile': {'id': 'B089C36A1B6', 'app_id': 'A0892UE22GN', 'name': 'ag2commsagent', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'deleted': False, 'updated': 1737225262, 'team_id': 'T088N5HLRTR'}, 'blocks': [{'type': 'rich_text', 'block_id': 'frMN', 'elements': [{'type': 'rich_text_section', 'elements': [{'type': 'emoji', 'name': 'mostly_sunny', 'unicode': '1f324-fe0f'}, {'type': 'text', 'text': \" Hey team! Today's weather forecast is in: It's a chilly 10°C with some strong cold winds! \"}, {'type': 'emoji', 'name': 'cold_face', 'unicode': '1f976'}, {'type': 'text', 'text': \" Don't forget to wrap up warm! \"}, {'type': 'emoji', 'name': 'scarf', 'unicode': '1f9e3'}, {'type': 'emoji', 'name': 'coffee', 'unicode': '2615'}]}]}]}, {'user': 'U088V1NKK3R', 'type': 'message', 'ts': '1738728738.219479', 'bot_id': 'B089C36A1B6', 'app_id': 'A0892UE22GN', 'text': \":rain_cloud::umbrella_with_rain_drops: Good morning, everyone! Today's weather forecast is: \\n\\n:thermometer: **20°C** \\n:cloud: Early storm with clearing showers later! \\n\\nDon't forget your umbrella! :rainbow::blush:\", 'team': 'T088N5HLRTR', 'bot_profile': {'id': 'B089C36A1B6', 'app_id': 'A0892UE22GN', 'name': 'ag2commsagent', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'deleted': False, 'updated': 1737225262, 'team_id': 'T088N5HLRTR'}, 'blocks': [{'type': 'rich_text', 'block_id': 'd4Qra', 'elements': [{'type': 'rich_text_section', 'elements': [{'type': 'emoji', 'name': 'rain_cloud', 'unicode': '1f327-fe0f'}, {'type': 'emoji', 'name': 'umbrella_with_rain_drops', 'unicode': '2614'}, {'type': 'text', 'text': \" Good morning, everyone! Today's weather forecast is: \\n\\n\"}, {'type': 'emoji', 'name': 'thermometer', 'unicode': '1f321-fe0f'}, {'type': 'text', 'text': ' **20°C** \\n'}, {'type': 'emoji', 'name': 'cloud', 'unicode': '2601-fe0f'}, {'type': 'text', 'text': \" Early storm with clearing showers later! \\n\\nDon't forget your umbrella! \"}, {'type': 'emoji', 'name': 'rainbow', 'unicode': '1f308'}, {'type': 'emoji', 'name': 'blush', 'unicode': '1f60a'}]}]}]}, {'user': 'U088V1NKK3R', 'type': 'message', 'ts': '1738728723.462599', 'bot_id': 'B089C36A1B6', 'app_id': 'A0892UE22GN', 'text': \":partly_sunny_rain::umbrella_with_rain_drops: Hey team! Today's weather forecast is 15°C and wet. Don't forget your umbrellas! :rain_cloud::rainbow: Stay dry! :umbrella_with_rain_drops::snowflake:\", 'team': 'T088N5HLRTR', 'bot_profile': {'id': 'B089C36A1B6', 'app_id': 'A0892UE22GN', 'name': 'ag2commsagent', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'deleted': False, 'updated': 1737225262, 'team_id': 'T088N5HLRTR'}, 'blocks': [{'type': 'rich_text', 'block_id': 'LmY', 'elements': [{'type': 'rich_text_section', 'elements': [{'type': 'emoji', 'name': 'partly_sunny_rain', 'unicode': '1f326-fe0f'}, {'type': 'emoji', 'name': 'umbrella_with_rain_drops', 'unicode': '2614'}, {'type': 'text', 'text': \" Hey team! Today's weather forecast is 15°C and wet. Don't forget your umbrellas! \"}, {'type': 'emoji', 'name': 'rain_cloud', 'unicode': '1f327-fe0f'}, {'type': 'emoji', 'name': 'rainbow', 'unicode': '1f308'}, {'type': 'text', 'text': ' Stay dry! '}, {'type': 'emoji', 'name': 'umbrella_with_rain_drops', 'unicode': '2614'}, {'type': 'emoji', 'name': 'snowflake', 'unicode': '2744-fe0f'}]}]}]}, {'user': 'U088V1NKK3R', 'type': 'message', 'ts': '1738728329.265739', 'bot_id': 'B089C36A1B6', 'app_id': 'A0892UE22GN', 'text': \":sunny::mostly_sunny: Good morning! The weather today is looking great! It's a sunny 25°C :thermometer:. But don’t forget, there might be a late storm :lightning: later in the day! Stay safe and enjoy your day! :rainbow::blush:\", 'team': 'T088N5HLRTR', 'bot_profile': {'id': 'B089C36A1B6', 'app_id': 'A0892UE22GN', 'name': 'ag2commsagent', 'icons': {'image_36': 'https://a.slack-edge.com/80588/img/plugins/app/bot_36.png', 'image_48': 'https://a.slack-edge.com/80588/img/plugins/app/bot_48.png', 'image_72': 'https://a.slack-edge.com/80588/img/plugins/app/service_72.png'}, 'deleted': False, 'updated': 1737225262, 'team_id': 'T088N5HLRTR'}, 'blocks': [{'type': 'rich_text', 'block_id': 'toH=', 'elements': [{'type': 'rich_text_section', 'elements': [{'type': 'emoji', 'name': 'sunny', 'unicode': '2600-fe0f'}, {'type': 'emoji', 'name': 'mostly_sunny', 'unicode': '1f324-fe0f'}, {'type': 'text', 'text': \" Good morning! The weather today is looking great! It's a sunny 25°C \"}, {'type': 'emoji', 'name': 'thermometer', 'unicode': '1f321-fe0f'}, {'type': 'text', 'text': '. But don’t forget, there might be a late storm '}, {'type': 'emoji', 'name': 'lightning', 'unicode': '1f329-fe0f'}, {'type': 'text', 'text': ' later in the day! Stay safe and enjoy your day! '}, {'type': 'emoji', 'name': 'rainbow', 'unicode': '1f308'}, {'type': 'emoji', 'name': 'blush', 'unicode': '1f60a'}]}]}]}], 'start_time': 'latest'}\n",
    "**********************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "slack_agent (to executor_agent):\n",
    "\n",
    "Here is a summary of the last five messages about daily weather from our Slack channel:\n",
    "\n",
    "1. **30°C, High Fire Danger**: \"Good morning! Today's weather is a sizzling 30°C! Stay hydrated and take care, especially with the high fire danger.\"\n",
    "   \n",
    "2. **10°C, Chilly with Strong Winds**: \"Hey team! Today's weather forecast is in: It's a chilly 10°C with some strong cold winds! Don't forget to wrap up warm!\"\n",
    "\n",
    "3. **20°C, Early Storm**: \"Good morning! Today's weather forecast is: 20°C with an early storm and clearing showers later. Don't forget your umbrella!\"\n",
    "\n",
    "4. **15°C, Wet**: \"Hey team! Today's weather forecast is 15°C and wet. Don't forget your umbrellas! Stay dry!\"\n",
    "\n",
    "5. **25°C, Sunny with Late Storm**: \"Good morning! The weather today is looking great at 25°C, but there might be a late storm later in the day. Stay safe and enjoy your day!\"\n",
    "\n",
    "### Summary of the Week's Weather:\n",
    "- There have been fluctuations in temperatures ranging from 10°C to 30°C over the past few days.\n",
    "- Weather conditions have included a mix of sunny days, chilly winds, and multiple instances of rain with early storms.\n",
    "- Overall, it's important to stay hydrated and be prepared for both sun and rain, with reminders to carry umbrellas on wet days.\n",
    "\n",
    "Let me know if you need further details!\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Telegram Send/Retrieve Messages\n",
    "\n",
    "### Tokens, Bots, Groups, and Channels\n",
    "The Telegram tools, `TelegramSendTool` and `TelegramRetrieveTool`, require authentication (API ID and hash) and target Bot/Group/Channel details in order to send/retrieve messages.\n",
    "\n",
    "The Telegram tool is different to the Discord and Slack tools in that you can send/retrieve messages to/from a bot channel, a group channel, a channel, or even your own private channel. This is all handled by giving the respective ID to the `chat_id` parameter when creating the tool.\n",
    "\n",
    "Here's how to establish your API ID and Hash:\n",
    "\n",
    "- [API ID and Hash](https://docs.telethon.dev/en/stable/basic/signing-in.html)\n",
    "\n",
    "If you want to create a bot, which is optional but allows you to send messages to the bot channel:\n",
    "\n",
    "- In Telegram, search for @BotFather\n",
    "- Click on @BotFather (make sure it's the correct one!) and click START\n",
    "- Message `/newbot`\n",
    "- Give it a name then a username\n",
    "- You'll then receive a token for your bot and you see the links below to get the ID for your bot\n",
    "\n",
    "There are four types of `chat_id`s you can send to:\n",
    "\n",
    "- Bot chat \n",
    "- Group chat\n",
    "- Channel chat\n",
    "- Private chat (noted in Telegram as `Saved Messages`)\n",
    "\n",
    "To find these the IDs for these, here are some references:\n",
    "\n",
    "- [Bot, Private, Channel, Group IDs](https://gist.github.com/nafiesl/4ad622f344cd1dc3bb1ecbe468ff9f8a)\n",
    "- [Group and Channel IDs](https://docs.b2core.b2broker.com/how-to-articles/manage-communication-platforms/how-to-get-telegram-chat-group-and-channel-identifiers)\n",
    "- [Group ID](https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id)\n",
    "\n",
    "It takes a bit of time to get this sorted, but once you have your token and IDs, you're ready to go!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "````mdx-code-block\n",
    ":::note\n",
    "You may be prompted for Telegram verification during the AG2 chat session. This typically happens when you first run it and is not required during subsequent runs.\n",
    ":::\n",
    "````"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sending and retrieving messages with Telegram\n",
    "\n",
    "Let's add the Telegram Send and Retrieve tools to an agent and have them executed by another agent in as simple 2-agent chat."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tools are available in the autogen.tools namespace\n",
    "from autogen import ConversableAgent\n",
    "from autogen.tools.experimental import TelegramRetrieveTool, TelegramSendTool\n",
    "\n",
    "llm_config = {\"model\": \"gpt-5-nano\", \"api_type\": \"openai\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create our two agents."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Our tool executor agent, which will run the tools once recommended by the telegram_agent, no LLM required\n",
    "executor_agent = ConversableAgent(\n",
    "    name=\"executor_agent\",\n",
    "    human_input_mode=\"NEVER\",\n",
    ")\n",
    "\n",
    "telegram_agent = ConversableAgent(\n",
    "    name=\"telegram_agent\",\n",
    "    llm_config=llm_config,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Prepare our authentication and target chat id details, then create our tools, and finally register them with the agents.\n",
    "\n",
    "For this send, we'll send it to a group using the group's ID in our `chat_id`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Authentication and target Chat ID (in this case a Group chat)\n",
    "api_id = \"123.....\"\n",
    "api_hash = \"a2e.............................\"\n",
    "_chat_id_group = \"-4712345678\"\n",
    "\n",
    "# Create our send tool\n",
    "telegram_send_tool = TelegramSendTool(api_id=api_id, api_hash=api_hash, chat_id=_chat_id_group)\n",
    "\n",
    "# Register it for recommendation by our Telegram agent\n",
    "telegram_send_tool.register_for_llm(telegram_agent)\n",
    "\n",
    "# Register it for execution by our executor agent\n",
    "telegram_send_tool.register_for_execution(executor_agent)\n",
    "\n",
    "# And the same for our our retrieve tool\n",
    "telegram_retrieve_tool = TelegramRetrieveTool(api_id=api_id, api_hash=api_hash, chat_id=_chat_id_group)\n",
    "telegram_retrieve_tool.register_for_llm(telegram_agent)\n",
    "telegram_retrieve_tool.register_for_execution(executor_agent)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's send a message to our Telegram channel, with a joke for the day.\n",
    "\n",
    "We'll limit it to 2 turns, allowing the Telegram agent to receive the request, construct and recommend the send tool, and then the executor agent to execute the tool, sending the message to the Telegram group."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "executor_agent.initiate_chat(\n",
    "    recipient=telegram_agent,\n",
    "    message=\"Let's send a message to Telegram giving them a joke for the day about AI agentic frameworks\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And here's the joke message it sent:\n",
    "\n",
    "![Telegram message](https://media.githubusercontent.com/media/ag2ai/ag2/refs/heads/main/notebook/commsplatforms_telegram_sentmsg.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the output:\n",
    "\n",
    "```console\n",
    "executor_agent (to telegram_agent):\n",
    "\n",
    "Let's send a message to Telegram giving them a joke for the day about AI agentic frameworks\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "telegram_agent (to executor_agent):\n",
    "\n",
    "***** Suggested tool call (call_QfDyh1mP2sb6uUe6k9geRZCJ): telegram_send *****\n",
    "Arguments: \n",
    "{\"message\":\"Why did the AI agentic framework apply for a job? \\n\\nBecause it wanted to prove it could be more than just a 'model' citizen!\"}\n",
    "******************************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> EXECUTING FUNCTION telegram_send...\n",
    "Call ID: call_QfDyh1mP2sb6uUe6k9geRZCJ\n",
    "Input arguments: {'message': \"Why did the AI agentic framework apply for a job? \\n\\nBecause it wanted to prove it could be more than just a 'model' citizen!\"}\n",
    "executor_agent (to telegram_agent):\n",
    "\n",
    "***** Response from calling tool (call_QfDyh1mP2sb6uUe6k9geRZCJ) *****\n",
    "Message sent successfully (ID: 98):\n",
    "Why did the AI agentic framework apply for a job? \n",
    "\n",
    "Because it wanted to prove it could be more than just a 'model' citizen!\n",
    "**********************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "telegram_agent (to executor_agent):\n",
    "\n",
    "I've sent the joke to Telegram! Here's what I shared:\n",
    "\n",
    "\"Why did the AI agentic framework apply for a job? Because it wanted to prove it could be more than just a 'model' citizen!\" \n",
    "\n",
    "If you need anything else, feel free to ask!\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's now retrieve the last 10 messages and get their IDs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "executor_agent.initiate_chat(\n",
    "    recipient=telegram_agent,\n",
    "    message=\"Retrieve the latest 10 messages from Telegram, getting the IDs and a one sentence summary of each.\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Output with our messages and IDs (including our joke):\n",
    "\n",
    "```console\n",
    "executor_agent (to telegram_agent):\n",
    "\n",
    "Retrieve the latest 10 messages from Telegram, getting the IDs and a one sentence summary of each.\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "telegram_agent (to executor_agent):\n",
    "\n",
    "***** Suggested tool call (call_ycoRcdigyNdRZL9JYynMakAw): telegram_retrieve *****\n",
    "Arguments: \n",
    "{\"maximum_messages\":10}\n",
    "**********************************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> EXECUTING FUNCTION telegram_retrieve...\n",
    "Call ID: call_ycoRcdigyNdRZL9JYynMakAw\n",
    "Input arguments: {'maximum_messages': 10}\n",
    "executor_agent (to telegram_agent):\n",
    "\n",
    "***** Response from calling tool (call_ycoRcdigyNdRZL9JYynMakAw) *****\n",
    "{'message_count': 10, 'messages': [{'id': '98', 'date': '2025-02-05T05:13:13+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"Why did the AI agentic framework apply for a job? \\n\\nBecause it wanted to prove it could be more than just a 'model' citizen!\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': None}, {'id': '92', 'date': '2025-02-04T04:47:38+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"🇳🇿 **New Zealand** Awaits!\\n\\nIn a land where mountains kiss the sky,  \\nAotearoa calls, where dreams can fly.  \\nWith valleys green and rivers clear,  \\nNature's beauty, so pristine and dear.\\n\\nFrom fjords of grandeur to beaches so wide,  \\nThe spirit of adventure, a thrilling ride.  \\nWith Maori tales and rich heritage,  \\nNew Zealand's charm, a vibrant stage.\\n\\nKiwi birds may wander, shy and free,  \\nIn this land of wonders, come explore with me!  \\nWith every sunrise, a bright new start,  \\nNew Zealand captures every heart!  \\n\\nIf you wish to know more of this magical land,  \\nJoin in the journey, together we'll stand! 🌄\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': [{'type': 'MessageEntityBold', 'offset': 5, 'length': 11}]}, {'id': '91', 'date': '2025-02-04T04:47:11+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"🇬🇭 **Ghana** is a vibrant West African country known for its rich history, diverse culture, and friendly people! \\n\\n🌍 From the bustling markets of Accra to the serene beaches of Cape Coast, there's so much to explore. \\n\\n🏴\\u200d☠️ Don't miss the historical forts and castles, which tell the poignant story of the transatlantic slave trade. \\n\\n🌿 Nature lovers can enjoy the beautiful landscapes of Kakum National Park, famous for its canopy walk.\\n\\n🎉 Immerse yourself in Ghanaian culture through its lively festivals, music, and delicious cuisine! \\n\\nIf you're interested in learning more or have questions, feel free to share! 🌟\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': [{'type': 'MessageEntityBold', 'offset': 5, 'length': 5}]}, {'id': '90', 'date': '2025-02-04T04:46:40+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"🌏 **Australia** is a unique and diverse country known for its stunning landscapes, vibrant cities, and rich culture! \\n\\n🏞️ From the beautiful beaches of the Gold Coast to the rugged outback, Australia offers something for every adventurer.\\n\\n🌸 The Great Barrier Reef, the world's largest coral reef system, is a must-see for nature lovers!\\n\\n🎨 Don't forget to explore Australia's Aboriginal heritage, which is one of the oldest living cultures in the world.\\n\\nIf you're planning a visit or just curious, feel free to ask questions or share your experiences! 🇦🇺\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': [{'type': 'MessageEntityBold', 'offset': 3, 'length': 9}]}, {'id': '85', 'date': '2025-02-04T03:39:52+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"🇦🇺 **Australia:** A land of diverse landscapes, vibrant cities, and rich cultures! \\n\\nFrom the stunning beaches of the Gold Coast to the unique wildlife of the Outback, Australia offers a plethora of experiences. Don't forget to explore its iconic landmarks like the Sydney Opera House and the Great Barrier Reef! 🐨🌊\\n\\nWhat are your favorite places or experiences in Australia? Share below!\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': None}, {'id': '83', 'date': '2025-02-02T23:30:13+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"Australia is a vast and diverse country known for its stunning landscapes, unique wildlife, and vibrant cities. Highlights include the iconic Sydney Opera House and the Great Barrier Reef, one of the world's natural wonders. The Outback offers breathtaking deserts and rugged beauty, while cities like Melbourne and Brisbane boast rich culture and culinary scenes. Australia is also home to fascinating creatures like kangaroos and koalas, making it a dream destination for nature lovers and adventure seekers.\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': None}, {'id': '77', 'date': '2025-02-02T23:18:45+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"Australia is a vast and diverse country known for its stunning landscapes, unique wildlife, and vibrant cities. Highlights include the iconic Sydney Opera House and the Great Barrier Reef, one of the world's natural wonders. The Outback offers breathtaking deserts and rugged beauty, while cities like Melbourne and Brisbane boast rich culture and culinary scenes. Australia is also home to fascinating creatures like kangaroos and koalas, making it a dream destination for nature lovers and adventure seekers.\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': None}, {'id': '71', 'date': '2025-02-01T21:13:39+00:00', 'from_id': 'PeerUser(user_id=8049516890)', 'text': \"Australia is renowned for its stunning natural beauty and diverse ecosystems. From the iconic Sydney Opera House and Great Barrier Reef to the vast Outback and unique wildlife, Australia offers a rich tapestry of experiences. Highlights include the vibrant cities of Sydney and Melbourne, the ancient rainforests of Queensland, and the breathtaking landscapes of Tasmania. The country also boasts a rich Indigenous heritage, with opportunities to learn about the Aboriginal culture and traditions. Whether you're exploring the rugged coastlines, relaxing on gorgeous beaches, or experiencing the local cuisine, Australia is a destination that captivates and inspires.\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': None}, {'id': '70', 'date': '2025-02-01T21:10:27+00:00', 'from_id': 'PeerUser(user_id=8049516890)', 'text': \"Australia is renowned for its stunning natural beauty and diverse ecosystems. From the iconic Sydney Opera House and Great Barrier Reef to the vast Outback and unique wildlife, Australia offers a rich tapestry of experiences. Highlights include the vibrant cities of Sydney and Melbourne, the ancient rainforests of Queensland, and the breathtaking landscapes of Tasmania. The country also boasts a rich Indigenous heritage, with opportunities to learn about the Aboriginal culture and traditions. Whether you're exploring the rugged coastlines, relaxing on gorgeous beaches, or experiencing the local cuisine, Australia is a destination that captivates and inspires.\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': None}, {'id': '69', 'date': '2025-02-01T21:10:05+00:00', 'from_id': 'PeerUser(user_id=8049516890)', 'text': \"Australia is renowned for its stunning natural beauty and diverse ecosystems. From the iconic Sydney Opera House and Great Barrier Reef to the vast Outback and unique wildlife, Australia offers a rich tapestry of experiences. Highlights include the vibrant cities of Sydney and Melbourne, the ancient rainforests of Queensland, and the breathtaking landscapes of Tasmania. The country also boasts a rich Indigenous heritage, with opportunities to learn about the Aboriginal culture and traditions. Whether you're exploring the rugged coastlines, relaxing on gorgeous beaches, or experiencing the local cuisine, Australia is a destination that captivates and inspires.\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': None}], 'start_time': 'latest'}\n",
    "**********************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "telegram_agent (to executor_agent):\n",
    "\n",
    "Here are the latest 10 messages retrieved from Telegram, along with their IDs and a brief summary of each:\n",
    "\n",
    "1. **Message ID: 98**  \n",
    "   **Date:** 2025-02-05  \n",
    "   **Summary:** A humorous joke about an AI agentic framework applying for a job to prove it could be more than just a \"model\" citizen.\n",
    "\n",
    "2. **Message ID: 92**  \n",
    "   **Date:** 2025-02-04  \n",
    "   **Summary:** A poetic invitation to explore the beauty and adventure of New Zealand, highlighting its natural landscapes and rich culture.\n",
    "\n",
    "3. **Message ID: 91**  \n",
    "   **Date:** 2025-02-04  \n",
    "   **Summary:** A description of Ghana, focusing on its vibrant culture, historical significance, and recommendations for exploring its markets and natural parks.\n",
    "\n",
    "4. **Message ID: 90**  \n",
    "   **Date:** 2025-02-04  \n",
    "   **Summary:** An overview of Australia, mentioning its diverse landscapes, the Great Barrier Reef, and Aboriginal heritage, encouraging exploration.\n",
    "\n",
    "5. **Message ID: 85**  \n",
    "   **Date:** 2025-02-04  \n",
    "   **Summary:** A brief highlight of Australia's landscapes, wildlife, and iconic landmarks, inviting others to share their favorite experiences in the country.\n",
    "\n",
    "6. **Message ID: 83**  \n",
    "   **Date:** 2025-02-02  \n",
    "   **Summary:** A detailed description of Australia, mentioning its iconic sites, diverse ecosystems, and cultural richness, appealing to adventure seekers.\n",
    "\n",
    "7. **Message ID: 77**  \n",
    "   **Date:** 2025-02-02  \n",
    "   **Summary:** Similar to the previous message, it emphasizes Australia’s stunning landscapes and wildlife, making it an appealing destination for travelers.\n",
    "\n",
    "8. **Message ID: 71**  \n",
    "   **Date:** 2025-02-01  \n",
    "   **Summary:** A rich narrative about Australia's natural beauty, historic sites, and cultural diversity, promoting the country's various experiential highlights.\n",
    "\n",
    "9. **Message ID: 70**  \n",
    "   **Date:** 2025-02-01  \n",
    "   **Summary:** A reiteration of Australia's ecosystems and heritage, inviting exploration of its remarkable landscapes and cultural experiences.\n",
    "\n",
    "10. **Message ID: 69**  \n",
    "    **Date:** 2025-02-01  \n",
    "    **Summary:** A message celebrating Australia’s diverse environments and cultural richness, encouraging engagement with the local culture and adventures.\n",
    "\n",
    "Let me know if you need any more details or further assistance!\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And, finally, let's get all messages after a given ID, in this case ID 85. This should return 4 messages (IDs 90, 91, 92 ,98)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "executor_agent.initiate_chat(\n",
    "    recipient=telegram_agent,\n",
    "    message=\"Retrieve all messages since message ID 85, summarising each.\",\n",
    "    max_turns=2,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Output:\n",
    "\n",
    "```console\n",
    "executor_agent (to telegram_agent):\n",
    "\n",
    "Retrieve all messages since message ID 85, summarising each.\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "telegram_agent (to executor_agent):\n",
    "\n",
    "***** Suggested tool call (call_ox9jlkhgJalPN2WR7uoxX17n): telegram_retrieve *****\n",
    "Arguments: \n",
    "{\"messages_since\":\"85\"}\n",
    "**********************************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> EXECUTING FUNCTION telegram_retrieve...\n",
    "Call ID: call_ox9jlkhgJalPN2WR7uoxX17n\n",
    "Input arguments: {'messages_since': '85'}\n",
    "executor_agent (to telegram_agent):\n",
    "\n",
    "***** Response from calling tool (call_ox9jlkhgJalPN2WR7uoxX17n) *****\n",
    "{'message_count': 4, 'messages': [{'id': '98', 'date': '2025-02-05T05:13:13+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"Why did the AI agentic framework apply for a job? \\n\\nBecause it wanted to prove it could be more than just a 'model' citizen!\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': None}, {'id': '92', 'date': '2025-02-04T04:47:38+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"🇳🇿 **New Zealand** Awaits!\\n\\nIn a land where mountains kiss the sky,  \\nAotearoa calls, where dreams can fly.  \\nWith valleys green and rivers clear,  \\nNature's beauty, so pristine and dear.\\n\\nFrom fjords of grandeur to beaches so wide,  \\nThe spirit of adventure, a thrilling ride.  \\nWith Maori tales and rich heritage,  \\nNew Zealand's charm, a vibrant stage.\\n\\nKiwi birds may wander, shy and free,  \\nIn this land of wonders, come explore with me!  \\nWith every sunrise, a bright new start,  \\nNew Zealand captures every heart!  \\n\\nIf you wish to know more of this magical land,  \\nJoin in the journey, together we'll stand! 🌄\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': [{'type': 'MessageEntityBold', 'offset': 5, 'length': 11}]}, {'id': '91', 'date': '2025-02-04T04:47:11+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"🇬🇭 **Ghana** is a vibrant West African country known for its rich history, diverse culture, and friendly people! \\n\\n🌍 From the bustling markets of Accra to the serene beaches of Cape Coast, there's so much to explore. \\n\\n🏴\\u200d☠️ Don't miss the historical forts and castles, which tell the poignant story of the transatlantic slave trade. \\n\\n🌿 Nature lovers can enjoy the beautiful landscapes of Kakum National Park, famous for its canopy walk.\\n\\n🎉 Immerse yourself in Ghanaian culture through its lively festivals, music, and delicious cuisine! \\n\\nIf you're interested in learning more or have questions, feel free to share! 🌟\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': [{'type': 'MessageEntityBold', 'offset': 5, 'length': 5}]}, {'id': '90', 'date': '2025-02-04T04:46:40+00:00', 'from_id': 'PeerUser(user_id=7746084224)', 'text': \"🌏 **Australia** is a unique and diverse country known for its stunning landscapes, vibrant cities, and rich culture! \\n\\n🏞️ From the beautiful beaches of the Gold Coast to the rugged outback, Australia offers something for every adventurer.\\n\\n🌸 The Great Barrier Reef, the world's largest coral reef system, is a must-see for nature lovers!\\n\\n🎨 Don't forget to explore Australia's Aboriginal heritage, which is one of the oldest living cultures in the world.\\n\\nIf you're planning a visit or just curious, feel free to ask questions or share your experiences! 🇦🇺\", 'reply_to_msg_id': None, 'forward_from': None, 'edit_date': None, 'media': False, 'entities': [{'type': 'MessageEntityBold', 'offset': 3, 'length': 9}]}], 'start_time': '85'}\n",
    "**********************************************************************\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "\n",
    ">>>>>>>> USING AUTO REPLY...\n",
    "telegram_agent (to executor_agent):\n",
    "\n",
    "Here are the summaries of the messages retrieved since message ID 85:\n",
    "\n",
    "1. **Message ID 98**:\n",
    "   - **Date**: February 5, 2025\n",
    "   - **Content**: A humorous joke about an AI agent applying for a job to prove it could be more than just a \"model\" citizen.\n",
    "\n",
    "2. **Message ID 92**:\n",
    "   - **Date**: February 4, 2025\n",
    "   - **Content**: A poetic description of New Zealand, highlighting its natural beauty, adventure opportunities, Maori heritage, and the charm of the country.\n",
    "\n",
    "3. **Message ID 91**:\n",
    "   - **Date**: February 4, 2025\n",
    "   - **Content**: An informative message about Ghana, showcasing its rich history, culture, markets, beautiful landscapes, and the importance of historical forts related to the transatlantic slave trade.\n",
    "\n",
    "4. **Message ID 90**:\n",
    "   - **Date**: February 4, 2025\n",
    "   - **Content**: A brief overview of Australia, emphasizing its stunning landscapes, vibrant cities, the Great Barrier Reef, and Aboriginal culture, inviting readers to ask questions or share experiences.\n",
    "\n",
    "--------------------------------------------------------------------------------\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And that's it! In this notebook we've seen how to send and receive messages across Discord, Slack, and Telegram!"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Adding Browsing Capabilities to AG2",
   "tags": [
    "tools",
    "browser-use",
    "webscraping",
    "function calling"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
